package com.labraff.redvideo;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ListFragment extends Fragment {


    private ListView listView;
    private List<news> newsList = new ArrayList<>();
    public news currentNews ;
    public news getCurrentNews(){
        return currentNews;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        MainActivity mainActivity = (MainActivity)getActivity();
        // 获取数据
        newsList = mainActivity.getNewsList();
        listView = (ListView) view.findViewById(R.id.listview);
        listView.setAdapter(new MyAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 数据选择
                currentNews = newsList.get(i);
                mainActivity.setCurrentNews(currentNews);

                if(!mainActivity.isTabletDevice(mainActivity)){
                    // 小屏则启动跳转
//                 从listFragment->detail_activity->detail_activity
                    Intent intent = new Intent(mainActivity,detail.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("newsItem", currentNews);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    //  大屏
                    DetailFragment detailFragment =
                            (DetailFragment) getActivity().getFragmentManager().findFragmentById(R.id.fragment_detail);
                    detailFragment.setData(currentNews,true);
                }
            }
        });
        return view;
    }



    //listView的适配器
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return newsList.size();
        }
        @Override
        public news getItem(int i) {
            news item = (news) newsList.get(i);
            return item;
        }
        @Override
        public long getItemId(int i) {
            return i;
        }
        public View getView(int index, View view, ViewGroup viewGroup) {
            view = View.inflate(getActivity(), R.layout.list_item, null);
            TextView newsTitle = (TextView)view.findViewById(R.id.news_title);
            TextView newsShowSource = (TextView) view.findViewById(R.id.news_showSource);
            TextView newsPublishTime = (TextView) view.findViewById(R.id.news_publishTime);
            TextView newsEditor = (TextView) view.findViewById(R.id.news_editor);
            TextView newsDuration = (TextView) view.findViewById(R.id.news_duration);
            news newsItem = getItem(index);
            newsTitle.setText(newsItem.getTitle());
            newsShowSource.setText(newsItem.getShowSource());
            newsPublishTime.setText(newsItem.getPublishTime());
            newsEditor.setText(newsItem.getEditor());
            newsDuration.setText(newsItem.getDuration());
            return view;
        }

 }



}